﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace Gui.Functies.Takenlijst_windows
{
    public partial class Removetaak : UserControl
    {
        private int projectID;
        string taak;
        string taakid;
        string status;
        private Hashtable takenBezig;
        private Hashtable takenKlaar;
        private Hashtable takenNietKlaar;
        MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");

        public Removetaak()
        {
            
        }

        public Removetaak(int projectnummer, Hashtable bezig, Hashtable klaar, Hashtable nietKlaar)
        {
            projectID = projectnummer;
            takenBezig = bezig;
            takenKlaar = klaar;
            takenNietKlaar = nietKlaar;
            InitializeComponent();
            getTaken();
        }

        public void getTaken()
        {
            comboBox1.Items.Clear();
            //taken weergeven die niet klaar zijn
            foreach (string key in takenNietKlaar.Keys)
            {
                comboBox1.Items.Add(String.Format("{0}", key, takenNietKlaar[key]));
            }

            //taken weergeven die bezig zijn
            foreach (string key in takenBezig.Keys)
            {
                comboBox1.Items.Add(String.Format("{0}", key, takenBezig[key]));
            }

            //taken weergeven die klaar zijn
            foreach (string key in takenKlaar.Keys)
            {
                comboBox1.Items.Add(String.Format("{0}", key, takenKlaar[key]));
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (taak == "")
            {
                MessageBox.Show("Er is geen taak geselecteerd");
            }
            else
            {      
                try
                {
                    taak = comboBox1.SelectedItem.ToString();
                    //id moet opgehaald worden aan de hand van de status van de taak
                    MySqlCommand checkStatus = new MySqlCommand("SELECT Status FROM taken WHERE ProjectID='" + projectID + "'AND Taak='" + taak + "'", grabConnection);
                    grabConnection.Open();
                    MySqlDataReader reader = checkStatus.ExecuteReader();

                    while (reader.Read())
                    {
                        status = reader["Status"].ToString();
                    }

                    reader.Dispose();
                    checkStatus.Dispose();
                    grabConnection.Close();
                }
                catch (Exception ex)
                {
                    if (ex is MySqlException || ex is NullReferenceException)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }

                deleteTask();
            }
        }

        public void deleteTask()
        {
            if (status == "Bezig")
            {
                taakid = takenBezig[taak].ToString();
            }
            else
            {
                if (status == "Klaar")
                {
                    taakid = takenKlaar[taak].ToString();
                }
                else
                {
                    if (status == "Niet klaar")
                    {
                        taakid = takenNietKlaar[taak].ToString();
                    }
                }
            }

            MySqlCommand deltaak = new MySqlCommand("DELETE FROM taken WHERE TaakID ='"+taakid+"'", grabConnection);

            try
            {
                grabConnection.Open();
                deltaak.ExecuteNonQuery();
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {
                grabConnection.Close();
                this.Controls.Clear();
                this.Controls.Add(new Takenlijst(projectID));
            }
        }
    }
}
